
import pygame
import sys

from pygame.locals import *


pygame.init()


size=width,height=500,500

# 指定窗口大小
screen = pygame.display.set_mode(size,RESIZABLE)

# 设置窗口标题
pygame.display.set_caption("我是测试")

bg=(255,255,255)


screen.fill(bg)


# 加载图片
girl=pygame.image.load("g.jpg")

girl_right = pygame.transform.flip(girl,True,False)



# 翻转
girl_r = pygame.transform.rotate(girl, 90)
girl_t = pygame.transform.rotate(girl, 180)
girl_l = pygame.transform.rotate(girl, 270)
girl_b = girl


# 当前渲染的图像
currentImg = girl_t

# 获得图像的完整矩形
position = currentImgRect = currentImg.get_rect()
distance = 10

speed=[distance,0]

fullscreen=False

fullscreen_size = pygame.display.list_modes()[0]
ratio=1.0

while True:

    for e in pygame.event.get():
        
        # 关闭
        if e.type==QUIT:
            sys.exit()

        # 按键
        if e.type==KEYDOWN:
            
            # 移动
            if e.key==K_LEFT or e.key==K_RIGHT or e.key==K_UP or e.key==K_DOWN:
                
                if e.key==K_LEFT:
                    speed=[-50,0]
                    currentImg=girl
                
                if e.key==K_RIGHT:
                    speed=[50,0]
                    currentImg=girl_right
                    
                if e.key==K_UP:
                    speed=[0,-50]
                    
                if e.key==K_DOWN:
                    speed=[0,50]

                # 移动图像
                position = position.move(speed)

            # 全屏
            if e.key==K_F11:
                
                fullscreen = not  fullscreen

                if fullscreen:
                    width,height=e.size=fullscreen_size
                    pygame.display.set_mode(fullscreen_size, FULLSCREEN|HWSURFACE)
                else:
                    pygame.display.set_mode(size)

            # 放大缩小
            if e.key==K_EQUALS or e.key == K_MINUS or e.key ==K_SPACE:
                
                if e.key == K_EQUALS and ratio<1.3:
                    ratio+=0.1
            
                if e.key == K_MINUS and ratio>0.7:
                    ratio-=0.1

                if e.key ==K_SPACE:
                    ratio=1.0

                currentImg= pygame.transform.smoothscale(
                        girl,
                        (int(position.width*ratio),int(position.height*ratio))
                    )

                # position = currentImg.get_rect()
                
        # 拉伸
        if e.type== VIDEORESIZE:
            
            # size = width,height=e.size
            # pygame.display.set_mode(e.size,RESIZABLE)
            
            # position = currentImg.get_rect()

            print("调整成功")


    position = position.move(speed)

    if position.right > width:
        currentImg = girl_r
        position = currentImgRect = currentImg.get_rect()
        position.left = width - currentImgRect.width
        position.top =currentImgRect.height
        speed=[0,distance]

    if position.bottom>height:
        currentImg= girl_b
        position = currentImgRect = currentImg.get_rect()
        position.left = width - currentImgRect.width
        position.top = height - currentImgRect.height
        speed=[-distance,0]
    
    if position.left <  0:
        currentImg = girl_l
        position = currentImgRect = currentImg.get_rect()
        position.top = height - currentImgRect.height
        position.left =  0
        speed=[0,-distance]

    if position.top<0 :
        currentImg = girl_t
        position = currentImgRect = currentImg.get_rect()
        position.bottom =currentImgRect.height
        speed=[distance,0]

    # print(position.left,position.right,position.top,position.bottom,"--->",width,height)

    # print(position.right > width,position.bottom>height,
    #     position.left <0,position.top< 0)

        
    # print(speed,position)

    # 填充背景
    screen.fill(bg)

    # 更新图像
    screen.blit(currentImg,position)

    # 更新界面
    pygame.display.flip()
    
    pygame.time.delay(50)
